package com.wanxi.repertory.service.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wanxi.repertory.service.api.dto.RepertoryUserDto;
import com.wanxi.repertory.service.api.dto.RepertoryUserPageDto;
import com.wanxi.repertory.service.api.feign.RepertoryUserService;
import com.wanxi.repertory.service.api.result.CommonResult;
import com.wanxi.repertory.service.api.entity.RepertoryUser;
import com.wanxi.repertory.service.core_service.RepertoryTotalService;
import com.wanxi.repertory.service.mapper.RepertoryUserMapper;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author:wgy
 * @Data：2022/1/18 14:29
 */
@RestController
@RequestMapping("user/repertory")
public class RepertoryUserServiceImpl implements RepertoryUserService {

    @Resource
    private RepertoryUserMapper repertoryUserMapper;

    @Resource
    private RepertoryTotalService repertoryTotalService;

    @Override
    @GetMapping("findRepertory")
    public CommonResult<PageInfo<RepertoryUser>> findRepertory(RepertoryUserPageDto repertoryUserPageDto) {
        PageHelper.startPage(repertoryUserPageDto.getPage(), repertoryUserPageDto.getPageSize());
        Long shopId = null;
        if (repertoryUserPageDto.getShopId() != null) {
            shopId = repertoryUserPageDto.getShopId();
        }
        List<RepertoryUser> RepertoryUserVoList = repertoryUserMapper.findRepertory(shopId);
        PageInfo<RepertoryUser> pageInfo = new PageInfo<>(RepertoryUserVoList);
        return CommonResult.success(pageInfo);
    }

    @Override
    @PutMapping("updateBatch")
    public CommonResult<Integer> updateBatch(List<RepertoryUserDto> list) {
        int i = repertoryUserMapper.updateBatch(list);
        if (i > 0) {
            return CommonResult.success(i);
        }
        return CommonResult.failed();
    }

    @Override
    @GetMapping("findRepertoryBySkuId")
    public CommonResult<Boolean> findRepertoryBySkuId(Long skuId) {
        Long repertoryBySkuId = repertoryUserMapper.findRepertoryBySkuId(skuId);
        if (repertoryBySkuId != null) {
            return CommonResult.success(repertoryBySkuId != null);
        }
        return CommonResult.failed(repertoryBySkuId != null);
    }

    @Override
    @ApiOperation("根据sku id查找店铺id")
    @ApiImplicitParam(name = "skuId", value = "查找店铺的sku id", dataType = "Long")
    @GetMapping("/sku/findShopIdBySkuId")
    public CommonResult<Long> findShopIdBySkuId(Long skuId) {
        return CommonResult.success(repertoryTotalService.findShopIdBySkuId(skuId));
    }
}
